<template>
	<view>
		<view class="search-bar">
			<uni-search-bar class="search-bar" bgColor="#FFFFFF" placeholder="搜索房间号" @confirm="search"
				v-model="keywords" @cancel="searchCancel" />
		</view>
		<view class="list-wrap">
			<view class="item card-bg" style="padding: 0rpx;" v-for="(item) in list_data" :key="item.id">
				<view style="width: 10rpx;"></view>
				<view class="dot-green" v-if="item.onlineState==1 && item.factoryCode=='dahua'  "></view>
				<view class="dot-grey"  v-if="item.onlineState==2 && item.factoryCode=='dahua' "></view>
				<text class="title" style="padding: 30rpx 20rpx;" @click="toDetail(item)">{{item.name}}</text>
				<view class="btn-wrap" v-if="item.onlineState==1 && item.factoryCode=='dahua' ">
					<button type="default" class="btn-open" @click="openDoor(item)">开门</button>
				</view>
			</view>
			<uni-load-more status="loading" v-show="isLoading" />
		</view>
		<xw-empty v-if="!list_data.length" v-on:refresh="onRefressh"></xw-empty>
		<uni-fab ref="fabgate" :pattern="pattern" :content="content" direction="horizontal" horizontal="left"
			@trigger="onMenuClick"></uni-fab>
	</view>
</template>

<script>
	import cfg from '@/utils/config.js'
	export default {
		data() {
			return {
				pattern: {
					icon: 'bars'
				},
				content: [{
					iconPath: '/static/ic_log.png',
					text: '日志',
				}, {
					iconPath: '/static/ic_add_circle.png',
					text: '添加',
				}, ],

				keywords: '',
				page: 1,
				isLoading: false,
				list_data: []
			}
		},
		onPullDownRefresh() {
			this.onRefressh()
			setTimeout(function() {
				uni.stopPullDownRefresh()
			}, 2000)
		},
		onReachBottom() {
			this.page++;
			this.isLoading = true
			this.getList()
		},
		onLoad() {
			uni.hideHomeButton()
		},
		onShow() {
			this.onRefressh()
		},
		methods: {
			openDoor(item) {
				uni.request({
					url: cfg.host + 'room/open/door/' + item.id,
					method: 'POST',
					success: (res) => {
						if (res.data.code == 0) {
							uni.showToast({
								icon: 'none',
								title: '门已开启'
							})
						} else {
							uni.showToast({
								icon: 'none',
								title: '开启失败'
							})
						}
					},
					fail: () => {
						uni.showToast({
							icon: 'none',
							title: '开启失败'
						})
					}
				})
			},
			search() {
				this.onRefressh()
			},
			searchCancel() {
				this.keywords = ''
				this.onRefressh()
			},
			toDetail(item) {
				uni.navigateTo({
					url: '/pages2/gate/detail?id=' + item.id
				})
			},
			onMenuClick(e) {
				if (e.index == 0) {
					uni.navigateTo({
						url: '/pages2/gate/log'
					})
				} else if (e.index == 1) {
					uni.navigateTo({
						url: '/pages2/gate/edit'
					})
				}
				this.$refs.fabgate.close()
			},
			onRefressh() {
				this.page = 1
				this.getList()
			},
			getList() {
				uni.request({
					url: cfg.host + 'room/page-query',
					method: 'POST',
					data: {
						'pageNo': this.page,
						'pageSize':20,
						'name': this.keywords
					},
					success: (res) => {
						if (res.data.code == 0) {
							let list = res.data.data.list || []
							if (this.page == 1) {
								this.list_data = list
							} else {
								this.list_data = this.list_data.concat(list)
							}
						}
					},
					complete: () => {
						this.isLoading = false
					}
				})
			},
		}
	}
</script>

<style lang="scss">
	.search-bar {
		position: sticky;
		top: 0;
		z-index: 10;
		background-color: #f5f5f5;
	}

	.list-wrap {
		position: relative;
		width: 100%;
		display: flex;
		flex-direction: column;
	}

	.item {
		display: flex;
		flex-direction: row;
		align-items: center;

		.dot-green {
			width: 16rpx;
			height: 16rpx;
			border-radius: 16rpx;
			background-color: green;
		}

		.dot-grey {
			width: 16rpx;
			height: 16rpx;
			border-radius: 16rpx;
			background-color: gray;
		}

		.title {
			flex: 2;
			font-size: 30rpx;
		}

		.btn-wrap {
			padding: 0rpx 40rpx;

			.btn-open {
				width: 120rpx;
				height: 50rpx;
				line-height: 50rpx;
				font-size: 26rpx;
			}
		}
	}
</style>